package com.fanjia.crm.web.controller;

import com.fanjia.crm.model.CURDResult;
import com.fanjia.crm.model.CourseCategory;
import com.fanjia.crm.model.CourseOrder;
import com.fanjia.crm.model.PageResult;
import com.fanjia.crm.service.ICourseCategoryService;
import com.fanjia.crm.service.ICourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by FanJia on 2020/9/16 15:46
 */
@Controller
@RequestMapping("/courseorder")
public class CourseOrderController {

    @Autowired
    ICourseOrderService courseOrderService;

    @Autowired
    ICourseCategoryService categoryService;

    @RequestMapping("/list")
    public String list(){
        return "/courseorder/list";
    }

    @RequestMapping("/add")
    public String add(Model model){
        //读取数据库中的课程分类，表
        List<CourseCategory> categoryList = categoryService.findAllList();
        model.addAttribute("categoryList",categoryList);
        System.out.println(categoryList);
        return "/courseorder/add";
    }

    @RequestMapping("/edit")
    public String edit(Model model,String order_id){
        System.out.println(order_id);
        CourseOrder order = courseOrderService.findByOrderId(order_id);
        System.out.println(order);
        model.addAttribute("order",order);
        return "/courseorder/edit";
    }

    @RequestMapping("/detail")
    public String detail(Model model, String order_id){

        CourseOrder order =  courseOrderService.findByOrderId(order_id);
        System.out.println(order_id);
        System.out.println(order);
        model.addAttribute("order",order);
        return "/courseorder/detail";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public CURDResult delete(String order_id){
        CURDResult result = new CURDResult();
       courseOrderService.deleteByOrderId(order_id);
        return result;
    }



    @RequestMapping("/save")
    @ResponseBody
    public CURDResult save(CourseOrder order){
        CURDResult result = new CURDResult();
        if(order.getOrder_id()==null || order.getOrder_id().length()==0){
            courseOrderService.save(order);
        }else{
            courseOrderService.update(order);
        }
        return result;
    }



    @RequestMapping("/listjson")
    @ResponseBody
    public PageResult<CourseOrder> listJson(CourseOrder condition,int page, int limit){
        System.out.println(condition);
        return courseOrderService.findPageResult(condition,page,limit);

    }


}
